<?php

class PdoVoter extends PdoConnect {

    public function create(voter $voter) {
        try {
            $this->open();
            $sql = "INSERT INTO `voter`(idsondage, idutilisat) VALUES(?, ?)";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $voter->getIdSondage(), PDO::PARAM_INT);
            $prep->bindValue(2, $voter->getIdUtilisat(), PDO::PARAM_INT);

            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function readAll() {
        try {
            $this->open();
            $sql = "SELECT * FROM `voter`";
            $prep = $this->pdo->prepare($sql);

            $prep->execute();
            $prep->setFetchMode(PDO::FETCH_ASSOC);


            return $prep;
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function readOne($IdSondage, $IdUtilisat) {
        try {
            $this->open();
            $sql = "SELECT * FROM `voter` WHERE idsondage=$IdSondage AND idutilisat=$IdUtilisat";
            $prep = $this->pdo->prepare($sql);

            $prep->execute();

            $voter = new Voter();
            $ligne = $prep->fetch(PDO::FETCH_OBJ);
            $voter->setIdSondage($ligne->IdSondage);
            $voter->setIdUtilisat($ligne->IdUtilisat);
            $voter->setDejaVoter($ligne->DejaVoter);


            return $voter;
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }
        public function exist($IdSondage, $IdUtilisat) {
        $this->open();
        $query = "SELECT * FROM voter WHERE idsondage=$IdSondage AND idutilisat=$IdUtilisat;";
        $statement = $this->pdo->query($query);
        return $statement->rowCount();
    }

}

